<?php

class ItemImage
{
	function getImageRealUrls($item_id, $image_name)
	{
		$hash_dir = self::getHashById($item_id);
		sys::sql("update items set view_count=view_count+1 where item_id='%d';", $item_id);
		
		return array(
			's' => ITEM_IMAGE_BASE_DIR_SIZE_SMALL.$hash_dir.'/'.$image_name,
			'm' => ITEM_IMAGE_BASE_DIR_SIZE_MEDIUM.$hash_dir.'/'.$image_name,
			'l' => ITEM_IMAGE_BASE_DIR_SIZE_LARGE.$hash_dir.'/'.$image_name,
			'o' => ITEM_IMAGE_BASE_DIR_ORIGINAL.$hash_dir.'/'.$image_name,
			);
	}
	
	function getHashById($hash_id, $filename="")
	{
		$hash_num_1 = 10;
		$hash_num_2 = 10;
		
		$len_1 = log($hash_num_1, 10);
		$len_2 = log($hash_num_2, 10);
		
		$dir_1 = $hash_id%$hash_num_1;
		$dir_2 = floor($hash_id/$hash_num_1)%$hash_num_2;
	
		$retval = $dir_2."/".$dir_1;

		if ($filename)
			$retval .= "/".$filename;
		
		return $retval;
	}
	
	
	/*
		multiply saving image to hash
			1/ original image directly copy to /o/[HASH DIR]/
			2/ save small(icon) size image to /s/[HASH DIR]/
			2/ save medium size image to /m/[HASH DIR]/
			2/ save large size image to /l/[HASH DIR]/
	*/
	function save($item_id, $tmp_file)
	{
		if (!file_exists($tmp_file))
			return false;
		
		$tmp_arr = explode("/", $tmp_file);
		$tmp_file_name = array_pop($tmp_arr);
		
		$hash_dir = self::getHashById($item_id);
		
		// save 1
		$dest_dir = ITEM_IMAGE_TRUNK_BASE_DIR.'/o/'.$hash_dir;
		if (sys::tryMakeDir($dest_dir))
		{
			
			if (!copy($tmp_file, $dest_dir.'/'.$tmp_file_name))
			{
				return false;
			}
		}
		else
		{
			return false;
		}
		
		// save 2
		$dest_dir = ITEM_IMAGE_TRUNK_BASE_DIR.'/s/'.$hash_dir;
		if (!self::saveWithAutoResize($tmp_file, $dest_dir, $tmp_file_name,
			200, 200, 100, 100))
		{
			return false;
		}
		
		// save 3
		$dest_dir = ITEM_IMAGE_TRUNK_BASE_DIR.'/m/'.$hash_dir;
		if (!self::saveWithAutoResize($tmp_file, $dest_dir, $tmp_file_name,
			400, 400, 200, 200))
		{
			return false;
		}
		
		// save 4
		$dest_dir = ITEM_IMAGE_TRUNK_BASE_DIR.'/l/'.$hash_dir;
		if (!self::saveWithAutoResize($tmp_file, $dest_dir, $tmp_file_name,
			800, 800, 400, 400))
		{
			return false;
		}
		
		return true;

	}
	
	// use as private
	function saveWithAutoResize($src_file, $dst_dir, $filename,
		$max_width, $max_height, $min_width=50, $min_height=50)
	{
		if (!sys::tryMakeDir($dst_dir))
		{
			return false;
		}		
		
		$image = new ResizeImage();
		if (!$image->load($src_file))
		{
			return false;
		}
		
		if (!$image->autoResize($max_width, $max_height, $min_width, $min_height))
		{
			return false;
		}
		
		if (!$image->save($dst_dir.'/'.$filename))
		{
			echo $dst_dir.'/'.$filename;
			return false;
		}
		
		return true;
	}
	

	function log()
	{
		foreach (func_get_args() as $s)
		{
			if (is_array($s))
			{
				$log[] = print_r($s, true);
			}
			else
			{
				$log[] = $s;
			}
		}
		
		echo trim(implode("\t", $log))."\n";
	}}